import axios from 'axios'
import {
	ElMessage
} from 'element-plus';
// 请求超时 TODO 性能优化后 修改至 30000
axios.defaults.timeout = 600000;
//axios.defaults.withCredentials=true;
// 添加一个请求拦截器
axios.interceptors.request.use(
	res => {
		// res.baseURL = baseUrl || '',
		res.params = res.params || {};
		if (res.headers['Content-Type'] == 'application/json' && res.method == 'post') {
			res.params = res.data || {};
		} else {
			res.params = res.params || {};
		}
		if (localStorage.getItem('loginToken')) {
			res.headers.common['Authorization'] = localStorage.getItem('loginToken');
		}
		return res;
	},
	err => {
		return Promise.reject(err);
	}
);

// 添加一个响应拦截器
axios.interceptors.response.use(
	res => {
		//loading.close()
		if (res.data.code) {
			if (res.data.code == "0") {
				return res.data;
			} else if (res.data.code == "00010001") {
				ElMessage({
					message: '接口请求失败',
					type: 'error',
				})
				setTimeout(() => {
					localStorage.clear();
					window.location.href = "/";
				}, 2000)
				return;
			} else if (res.data.code == '00010000') { //密码修改
				ElMessage({
					message: '接口请求失败',
					type: 'error',
				})
				return;
			} else if (res.data.code == '999999') {
				if (process.env.VUE_APP_CURRENTMODE === 'production' && res.data.msg.includes('Exception')) {
					ElMessage({
						message: '接口请求失败',
						type: 'error',
					})
				} else {
					ElMessage({
						message: res.data.msg,
						type: 'error',
					})
				}
				return res.data;
			} else if (res.data.code == '000000') {
				return res.data.data;
			} else if (res.data.code == '00010020') {
				//登录失败不展示全局提示
				return res.data
			} else {
				ElMessage({
					message: res.data.msg,
					type: 'error',
				})
				return res.data;
			}
		} else {
			return res.data;
		}
	},
	error => {
		if (String(error).indexOf("timeout") > -1) {
			ElMessage({
				message: '请求超时',
				type: 'error',
			})
			return;
		} else if (String(error).indexOf("Network Error") > -1) {
			ElMessage({
				message: '网络异常，请检查网络',
				type: 'error',
			})
		} else {
			ElMessage({
				message: '服务异常，请稍后再试',
				type: 'error',
			})
		}
		console.log(error)
		if (error.response.status) {
			return Promise.reject(error.response);
		}
	}
);


export const postHeaderJson = (url, params) => {
	return new Promise((resolve, reject) => {
		axios.post(url, params, {
			headers: {
				'Content-Type': 'application/json'
			},
			emulateJSON: true,
		}).then(res => {
			resolve(res);
		}, err => {
			reject(err)
		})
	})
}
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 *
 */
export const get = (url, params, obj = {}) => {
	return new Promise((resolve, reject) => {
		axios
			.get(url, {
				params: params
			}, obj)
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const post = (url, params, obj = {}) => {
	//以x-www-form-urlencoded 格式post数据的时候，需要使用qs.stringify()
	return new Promise((resolve, reject) => {
		axios
			.post(url, params, obj)
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const postFile = (url, params) => {
	return new Promise((resolve, reject) => {
		axios
			.post(url, params, {
				headers: {
					"Content-Type": "multipart/form-data;charset=utf-8"
				}
			})
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};
/**
 * postForm  
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const postForm = (url, params) => {
	//以x-www-form-urlencoded 格式post数据的时候，需要使用qs.stringify()
	return new Promise((resolve, reject) => {
		axios({
				url: url,
				method: "post",
				data: params,
				transformRequest: [
					function(data) {
						// Do whatever you want to transform the data
						let ret = "";
						for (let it in data) {
							const value = data[it] !== undefined ? data[it] : ''
							ret += `&${encodeURIComponent(it)}=${encodeURIComponent(value)}`
						}
						return ret.slice(1)
					}
				],
				headers: {
					"Content-Type": "application/x-www-form-urlencoded"
					//   'Content-Type': 'multipart/form-data'
				}
			})
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};
/**
 * put 对应put请求
 * @param {String} url
 * @param {Object} data
 */
export const put = (url, params, obj = {}) => {
	return new Promise((resolve, reject) => {
		axios
			.put(url, params, {
				headers: {
					"Content-Type": "application/json"
				},
				...obj
			})
			.then(
				response => {
					resolve(response);
				},
				err => {
					reject(err);
				}
			);
	});
};
/**
 * delete方法，delete请求
 * @param {String} url
 * @param {Object} params
 */

export const delet = (url, params = {}) => {
	return new Promise((resolve, reject) => {
		axios
			.delete(url, {
				params: params
			})
			.then(response => {
				resolve(response);
			})
			.catch(err => {
				reject(err);
			});
	});
};
